package com.sky.service.admin.impl;

import cn.hutool.core.date.DateException;
import com.sky.constant.MessageConstant;
import com.sky.mapper.rider.CourierMapper;
import com.sky.pojo.Courier;
import com.sky.pojo.CourierTradeOrder;
import com.sky.properties.ShortUriProperties;
import com.sky.service.admin.CommonService;
import com.sky.service.rider.CourierService;
import com.sky.utils.AliOssUtil;
import com.sky.utils.MapUtils;
import com.sky.utils.ShortUriGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private CourierMapper courierMapper;


    @Override
    public String upload(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String lastName = fileName.substring(fileName.lastIndexOf("."));
        fileName =  UUID.randomUUID().toString()+ lastName;
        return  aliOssUtil.upload(file.getBytes(), fileName);
    }

    @Override
    public String loadRouteInfo(String tradeNo, Long courierId) {
        if (null==tradeNo ||null==courierId){
            throw new DateException(MessageConstant.DATA_ERROR);
        }
        CourierTradeOrder courierTradeOrder = courierMapper.selectTradeOrderByTradeNo(tradeNo);
        if (null==courierTradeOrder){
            throw new DateException(MessageConstant.DATA_ERROR);
        }
        //客户地址
        String customerAddress = MapUtils.addressToLnglat(courierTradeOrder.getCustomerAddress());
        //商家地址
        String shopAddress = MapUtils.addressToLnglat(courierTradeOrder.getShopAddress());
        //根据骑手id查询骑手
        Courier courier = courierMapper.selectById(courierId);

        //拼接字符串
        return customerAddress+"_"+shopAddress+"_"+courier.getCurLocation();
    }
}
